﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace LabyCLI
{
    public partial class frmSettings : Form
    {
        private Font _font = Properties.Settings.Default.Terminal_Font;

        public frmSettings()
        {
            InitializeComponent();
            
            //Load the settings onto the form.
            LoadSettings();
        }

        /// <summary>
        /// This is where our form reads the values from the settings 
        /// and adds them to the form.
        /// </summary>
        private void LoadSettings()
        {
            btn_SetBackgroundColor.BackColor = Properties.Settings.Default.Terminal_BackgroundColor;
            btn_SetTextColor.BackColor = Properties.Settings.Default.Terminal_TextColor;
            btn_SetCommandBackgroundColor.BackColor = Properties.Settings.Default.Terminal_CommandBackgroundColor;
            btn_SetCommandTextColor.BackColor = Properties.Settings.Default.Terminal_CommandTextColor;

            numericUpDown_FormWidth.Value = Properties.Settings.Default.Terminal_FormSize.Width;
            numericUpDown_FormHeight.Value = Properties.Settings.Default.Terminal_FormSize.Height;
            txtbox_StartDirectory.Text = Properties.Settings.Default.Terminal_StartDirectory;
            chkbox_FolderContextMenu.Checked = Properties.Settings.Default.LabyCLI_FolderContextMenu;
            chkbox_WrapText.Checked = Properties.Settings.Default.Terminal_WordWrap;
            chkbox_ShowMenuBar.Checked = Properties.Settings.Default.LabyCLI_ShowMenuBar;

            #region Shortcuts Tab

            chkbox_UseDesktopShortcut.Checked = Properties.Settings.Default.LabyCLI_UseDesktopShortcut;
            groupBox_UseDesktopShortcut.Enabled = Properties.Settings.Default.LabyCLI_UseDesktopShortcut;

            chkbox_UseDesktopShortcut_OnKeyedShortcut.Checked = Properties.Settings.Default.LabyCLI_UseDesktopShortcut_KeyedShortcut;
            groupBox_UseDesktopShortcut_OnKeyedShortcut.Enabled = Properties.Settings.Default.LabyCLI_UseDesktopShortcut_KeyedShortcut;

            chkbox_MinimizeToTray.Checked = Properties.Settings.Default.LabyCLI_MinimizeToTray;
            groupBox_MinimizeToTray.Enabled = Properties.Settings.Default.LabyCLI_MinimizeToTray;

            chkbox_MinimizeToTray_OnKeyedShortcut.Checked = Properties.Settings.Default.LabyCLI_MinimizeToTray_KeyedShortcut;
            groupBox_MinimizeToTray_OnKeyedShortcut.Enabled = Properties.Settings.Default.LabyCLI_MinimizeToTray_KeyedShortcut;

            #endregion

            lbl_TextExampleFont.Font = lbl_CommandExampleFont.Font = new Font(Properties.Settings.Default.Terminal_Font.Name, 25.2f);
        }

        /// <summary>
        /// Saving our settings for later use.
        /// </summary>
        private void SaveSettings()
        {
            Properties.Settings.Default.Terminal_TextColor = btn_SetTextColor.BackColor;
            Properties.Settings.Default.Terminal_BackgroundColor = btn_SetBackgroundColor.BackColor;
            Properties.Settings.Default.Terminal_CommandTextColor = btn_SetCommandTextColor.BackColor;
            Properties.Settings.Default.Terminal_CommandBackgroundColor = btn_SetCommandBackgroundColor.BackColor;
            Properties.Settings.Default.Terminal_FormSize = new Size((int)numericUpDown_FormWidth.Value, (int)numericUpDown_FormHeight.Value);

            Properties.Settings.Default.Terminal_Font = _font;

            Properties.Settings.Default.LabyCLI_FolderContextMenu = chkbox_FolderContextMenu.Checked;
            Properties.Settings.Default.Terminal_WordWrap = chkbox_WrapText.Checked;
            Properties.Settings.Default.LabyCLI_ShowMenuBar = chkbox_ShowMenuBar.Checked;

            #region Shortcuts Tab

            Properties.Settings.Default.LabyCLI_UseDesktopShortcut = chkbox_UseDesktopShortcut.Checked;
            Properties.Settings.Default.LabyCLI_UseDesktopShortcut_KeyedShortcut = chkbox_UseDesktopShortcut_OnKeyedShortcut.Checked;

            Properties.Settings.Default.LabyCLI_MinimizeToTray = chkbox_MinimizeToTray.Checked;
            Properties.Settings.Default.LabyCLI_MinimizeToTray_KeyedShortcut = chkbox_MinimizeToTray_OnKeyedShortcut.Checked;

            #endregion

            //Checking whether our startdirectory textbox is empty before applying the settings.
            if (!string.IsNullOrEmpty(txtbox_StartDirectory.Text))
            {
                Properties.Settings.Default.Terminal_StartDirectory = txtbox_StartDirectory.Text;
            }

            TerminalUtilities utils = new TerminalUtilities();

            //This checks our Properties.Settings values for Properties.Settings.Default.LabyCLI_FolderContextMenu
            if (Properties.Settings.Default.LabyCLI_FolderContextMenu)
            {
                //Generate the context menu.
                utils.GenerateGlobalContextMenu();
            }
            else
            {
                //Remove the context menu.
                utils.RemoveGlobalContextMenu();
            }

            if (Properties.Settings.Default.LabyCLI_UseDesktopShortcut)
            {
                //Remove shortcut.
                utils.RemoveDesktopShortcut();

                //Create the shortcut.

                if (Properties.Settings.Default.LabyCLI_UseDesktopShortcut_KeyedShortcut)
                {
                    utils.GenerateDesktopShortcut(txtbox_UseDesktopShortcut_HotKey.Text);
                }
                else
                {
                    utils.GenerateDesktopShortcut();
                }
            }
            else
            {
                //Remove shortcut.
                utils.RemoveDesktopShortcut();
            }

            //Save settings to file.
            Properties.Settings.Default.Save();
        }

        private void btn_SetTextColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.FullOpen = true;
            colorDialog.Color = btn_SetTextColor.BackColor;

            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                btn_SetTextColor.BackColor = colorDialog.Color;
            }
        }

        private void btn_SetBackgroundColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.FullOpen = true;
            colorDialog.Color = btn_SetBackgroundColor.BackColor;

            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                btn_SetBackgroundColor.BackColor = colorDialog.Color;
            }
        }

        private void btn_SetCommandTextColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.FullOpen = true;
            colorDialog.Color = btn_SetCommandTextColor.BackColor;

            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                btn_SetCommandTextColor.BackColor = colorDialog.Color;
            }
        }

        private void btn_SetCommandBackgroundColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.FullOpen = true;
            colorDialog.Color = btn_SetCommandBackgroundColor.BackColor;

            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                btn_SetCommandBackgroundColor.BackColor = colorDialog.Color;
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if ((btn_SetBackgroundColor.BackColor.GetBrightness() + btn_SetTextColor.BackColor.GetBrightness()) / 2 >= 0.8f || (btn_SetBackgroundColor.BackColor.GetBrightness() + btn_SetTextColor.BackColor.GetBrightness()) / 2 <= 0.2f)
            {
                MessageBox.Show(Application.ProductName + " has detected that your terminal background and foreground colors looks very similar. You might not be able to read the text on the terminal.\n\n" + (btn_SetBackgroundColor.BackColor.GetBrightness() + btn_SetTextColor.BackColor.GetBrightness()) / 2 * 100 + "% similarity, in brightness, detected.");
            }
            else if ((btn_SetCommandBackgroundColor.BackColor.GetBrightness() + btn_SetCommandTextColor.BackColor.GetBrightness()) / 2 >= 0.8f || (btn_SetCommandBackgroundColor.BackColor.GetBrightness() + btn_SetCommandTextColor.BackColor.GetBrightness()) / 2 <= 0.2f)
            {
                MessageBox.Show(Application.ProductName + " has detected that your terminal background and foreground colors looks very similar. You might not be able to read the text on the terminal.\n\n" + (btn_SetCommandBackgroundColor.BackColor.GetBrightness() + btn_SetCommandTextColor.BackColor.GetBrightness()) / 2 * 100 + "% similarity, in brightness, detected.");
            }

            SaveSettings();
        }

        private void btn_SetStartDirectoryBrowseFolders_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                txtbox_StartDirectory.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btn_SetFormSizeDefault_Click(object sender, EventArgs e)
        {
            numericUpDown_FormHeight.Value = 510;
            numericUpDown_FormWidth.Value = 870;
        }

        private void btn_SwapDefaultColorScheme_Click(object sender, EventArgs e)
        {
            Color BackgroundColor = btn_SetBackgroundColor.BackColor;
            Color TextColor = btn_SetTextColor.BackColor;

            btn_SetTextColor.BackColor = BackgroundColor;
            btn_SetBackgroundColor.BackColor = TextColor;
        }

        private void btn_SwapCommandColorScheme_Click(object sender, EventArgs e)
        {
            Color BackgroundColor = btn_SetCommandBackgroundColor.BackColor;
            Color TextColor = btn_SetCommandTextColor.BackColor;

            btn_SetCommandTextColor.BackColor = BackgroundColor;
            btn_SetCommandBackgroundColor.BackColor = TextColor;
        }

        private void txtbox_StartDirectory_TextChanged(object sender, EventArgs e)
        {
            if (Directory.Exists(Environment.ExpandEnvironmentVariables(txtbox_StartDirectory.Text)))
            {
                picbox_SpellCheck.Image = Properties.Resources.spell_check;
            }
            else
            {
                picbox_SpellCheck.Image = Properties.Resources.spell_check_error;
            }
        }

        private void chkbox_UseDesktopShortcut_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_UseDesktopShortcut.Enabled = chkbox_UseDesktopShortcut.Checked;
        }

        private void chkbox_UseDesktopShortcut_OnKeyedShortcut_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_UseDesktopShortcut_OnKeyedShortcut.Enabled = chkbox_UseDesktopShortcut_OnKeyedShortcut.Checked;
        }

        private void chkbox_MinimizeToTray_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_MinimizeToTray.Enabled = chkbox_MinimizeToTray.Checked;
        }

        private void chkbox_MinimizeToTray_OnKeyedShortcut_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_MinimizeToTray_OnKeyedShortcut.Enabled = chkbox_MinimizeToTray_OnKeyedShortcut.Checked;
        }

        private void txtbox_UseDesktopShortcut_HotKey_MouseUp(object sender, MouseEventArgs e)
        {
            txtbox_UseDesktopShortcut_HotKey.SelectAll();
        }

        private void txtbox_MinimizeToTray_HotKey_MouseUp(object sender, MouseEventArgs e)
        {
            txtbox_MinimizeToTray_HotKey.SelectAll();
        }

        private void ColorButtons_BackColorChanged(object sender, EventArgs e)
        {
            lbl_TextExampleFont.ForeColor = btn_SetTextColor.BackColor;
            lbl_TextExampleFont.BackColor = btn_SetBackgroundColor.BackColor;

            lbl_CommandExampleFont.ForeColor = btn_SetCommandTextColor.BackColor;
            lbl_CommandExampleFont.BackColor = btn_SetCommandBackgroundColor.BackColor;
        }

        #region DESCRIPTION - General Tab

        private void chkbox_FolderContextMenu_MouseEnter(object sender, EventArgs e)
        {
            lbl_Description_GeneralTab.Text = "Adds a \"Open in LabyCLI\" option to the menu, when right clicking folders. LabyCLI will then open with the current directory sat as being the choosen folder.";
        }

        private void chkbox_FolderContextMenu_MouseLeave(object sender, EventArgs e)
        {
            lbl_Description_GeneralTab.Text = "Position your mouse over an element to view a description.";
        }

        private void chkbox_WrapText_MouseEnter(object sender, EventArgs e)
        {
            lbl_Description_GeneralTab.Text = "Text wrap makes sure you do not have to scroll sideways to look at the output of the terminal. While activated the terminal distorts the text to fit the window in a vertical direction.";
        }

        private void chkbox_WrapText_MouseLeave(object sender, EventArgs e)
        {
            lbl_Description_GeneralTab.Text = "Position your mouse over an element to view a description.";
        }

        private void chkbox_ShowMenuBar_MouseEnter(object sender, EventArgs e)
        {
            lbl_Description_GeneralTab.Text = "Determines whether the menu bar is shown on the terminal. Having the menu bar provides you with plenty of features while showing you the corresponding keyboard shortcuts. Though if you can remember the shortcuts there is no reason not to turn this feature off.";
        }

        private void chkbox_ShowMenuBar_MouseLeave(object sender, EventArgs e)
        {
            lbl_Description_GeneralTab.Text = "Position your mouse over an element to view a description.";
        }

        #endregion

        private void btn_FontDialog_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog = new FontDialog();

            //Setting up the dialog before use.
            fontDialog.ShowHelp = false;
            fontDialog.FixedPitchOnly = true;
            fontDialog.ShowColor = false;
            fontDialog.ShowEffects = false;
            fontDialog.MinSize = 10;
            fontDialog.MaxSize = 10;

            //Show the dialog.
            if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                _font = fontDialog.Font;
                lbl_TextExampleFont.Font = lbl_CommandExampleFont.Font = new Font(fontDialog.Font.Name, 25.2f);
            }
        }
    }
}
